#!/bin/sh

#################################################################
#  This program is the Confidential and Proprietary product of 
#  Cadence Design Systems.  Any unauthorized use, reproduction 
#  or transfer of this program is strictly prohibited. 
#  
#  Copyright (c) 1989 - 1995, Cadence Design Systems, Inc. 
#  All rights reserved. 
#  
#  Filename:  vlog2alt
#  Purpose:   generates ALTERA EDIF 2 0 0 netlist from verilog
#  Author:    naji@cds9255.cadence.com
#  Date:      February 1995
#  
#  History:   
#################################################################

#
# set program variables
#

program="`basename $0`"
version="1.1"
workdir="`pwd`"
cdsroot="${CDS_ROOT:-`cds_root $0`}"

#
# set the Altera library & map file paths
#

maxPffName="concept2alt.pff"
lpmLibName="alt_lpm"
synLibName="alt_syn"
maxLibName="alt_max2"
maxLmfName="cadence.lmf"

#
# define function that is used to check whether a 5.X
# library is analyzed
#

chk_lib_path() {

	for file in `/bin/ls $1`; do

		if [ -d "$1/$file" ]; then
	
			if [ -d "$1/$file/hdl" ]; then
				return 0
			fi
		fi

	done

	return 1
}

#
# define function that is used get the path to a 5.X
# library
#

get_lib_path() {

	libname=$1
	libpath=$2

	if [ "$ALT_LIBS_PATH" ]; then

		p="$ALT_LIBS_PATH/$libname"

		if [ ! -d $p ]; then
			echo "Error: cannot find library - $p" >&2
			echo ">>> Check environment variable ALT_LIBS_PATH." >&2
			return 1
		fi

		chk_lib_path $p
		status=$?
 
		if [ "$status" -ne 0 ]; then
			echo "Error: invalid library - $p" >&2
			echo ">>> Library must be analyzed before it can be used." >&2
			return 1
		fi

		eval "$libpath=$p"
		return 0
	fi

	if [ "$ALT_HOME" ]; then

		p="$ALT_HOME/simlib/concept/$libname"

		if [ -d $p ]; then

			chk_lib_path $p
			status=$?

			if [ "$status" -eq 0 ]; then
				eval "$libpath=$p"
				return 0
			fi
		fi
	fi

	p="${cdsroot}/lib/$libname"

	if [ ! -d $p ]; then
		echo "Error: cannot find library - $p" >&2
		echo ">>> Check the current CDS hierarchy." >&2
		return 1
	fi

	chk_lib_path $p
	status=$?

	if [ "$status" -ne 0 ]; then
		echo "Error: invalid library - $p" >&2
		echo ">>> Library must be analyzed before it can be used." >&2
		return 1
	fi

	eval "$libpath=$p"
	return 0
}

#
# get path to Altera data files
#

if [ "$ALT_HOME" ]; then

	maxPffPath="${ALT_HOME}/cadence/bin/${maxPffName}"
 
	if [ ! -f $maxPffPath ]; then
		maxPffPath="${cdsroot}/share/library/altera/data/${maxPffName}"
	fi
 
	maxLmfPath="${ALT_HOME}/lmf/${maxLmfName}"
 
	if [ ! -f $maxLmfPath ]; then
		maxLmfPath="${cdsroot}/share/library/altera/data/${maxLmfName}"
	else
		maxLmfPath="${maxLmfName}"
	fi

else

	maxPffPath="${cdsroot}/share/library/altera/data/${maxPffName}"
	maxLmfPath="${cdsroot}/share/library/altera/data/${maxLmfName}"
fi

#
# define usage funtion
#
 
usage_exit() {
cat << EOF >&2
 
Usage: $program design [-device name] [-family name] [-genacf]
       [-help] [-log filename] [-rundir dirname] [-verbose] 
       [-vfiles filenames]

       -device name       Specify device name
       -family name       Specify family name
       -genacf            Generate ACF file
       -help              Print usage message
       -log filename      Specify log file name (default: ${program}.log)
       -rundir dirname    Specify run directory name (default: altera.run)
       -verbose           Display messages
       -vfiles filenames  Specify verilog file names (default: {design}.v)

Example: $program top -vfiles top.v block1.v block2.v

EOF
exit 2
}

#
# define function to create full pathnames
#
 
full_path() {
 
if pathname=`dirname $1` ; then
	if filename=`basename $1` ; then
		pathname=`(cd $pathname; pwd)`
		echo $pathname/$filename
		return
	fi
fi
 
echo "Error: illegal file name - $1" >&2
exit 1
}

#
# process command-line arguments
#
 
while [ $# -gt 0 ]; do
 
	case $1 in

		-device)	if [ ! "$2" ]; then
						echo "Error: -device option requires an argument" >&2
						usage_exit
					fi

					device=$2 ; vlist="off" ; shift ; shift ;;

		-family)	if [ ! "$2" ]; then
						echo "Error: -family option requires an argument" >&2
						usage_exit
					fi

					family=$2 ; vlist="off" ; shift ; shift ;;

		-genacf)	genACF="yes" ; vlist="off" ; shift ;;

		-help)		usage_exit ;;

		-log)		if [ ! "$2" ]; then
						echo "Error: -log option requires an argument" >&2
						usage_exit
					fi

					logfile=`full_path $2` ; vlist="off" ; shift ; shift ;;

		-rundir)	if [ ! "$2" ]; then
						echo "Error: -rundir option requires an argument" >&2
						usage_exit
					fi

					rundir=`full_path $2` ; vlist="off" ; shift ; shift ;;

		-verbose)	verbose="yes" ; vlist="off" ; shift ;;

		-vfiles)	vlist="on" ; shift ;;

		--)			vlist="off" ; shift ;;

		-*)			echo "Error: unrecognized option '$1'" >&2
					usage_exit ;;

		*)			if [ "$vlist" = "on" ]; then
						vlogfiles="$vlogfiles `full_path $1`"
					else
						design=$1
					fi

					shift ;;
	esac

done

#
# check command-line arguments
#

if [ ! "$design" ]; then
	echo "Error: must specify design name" >&2
	usage_exit
fi

if [ ! "$vlogfiles" ]; then
	vlogfiles="${workdir}/${design}.v"
fi

if [ ! "$rundir" ]; then
	rundir="${workdir}/altera.run"
fi

if [ ! "$logfile" ]; then
	logfile="${workdir}/${program}.log"
fi

#
# print program and copyright information
#

echo "Running $program version $version - `date`." | tee $logfile
echo "Copyright (c) 1995 Cadence Design Systems. All Rights Reserved." | tee -a $logfile
echo "" | tee -a $logfile

#
# create run directory if it does not exists
#
 
if [ ! -d $rundir ]; then
 
	mkdir -p $rundir >/dev/null 2>&1
 
	if [ $? != 0 ]; then
		echo "Error: cannot create run directory - $rundir" >&2
		exit 1
	fi
fi

#
# go to run directory
#
 
cd $rundir > /dev/null 2>&1

#
# get path to the Altera 5.X libraries
#

get_lib_path ${lpmLibName} lpmLibPath
 
if [ "$?" -ne 0 ]; then
	exit 1
fi

get_lib_path ${synLibName} synLibPath

if [ "$?" -ne 0 ]; then
	 exit 1
fi

get_lib_path ${maxLibName} maxLibPath

if [ "$?" -ne 0 ]; then
	exit 1
fi
 
#
# create the 5.X cds.lib file
#
 
echo "define $lpmLibName $lpmLibPath" > cds.lib
echo "define $maxLibName $maxLibPath" >> cds.lib
echo "define $synLibName $synLibPath" >> cds.lib
echo "define ${design}_lib ./${design}_lib" >> cds.lib

#
# analyze the design
#

echo "Analyzing the design ..." | tee -a $logfile

if [ "$verbose" ]; then
	echo ""
	van -lib ${design}_lib $vlogfiles 2>&1
else
	echo "" >> $logfile
	van -lib ${design}_lib $vlogfiles >>$logfile 2>&1
fi

if [ $? != 0 ]; then
	echo ""
	echo "Error: Failed to analyze the design."
	exit 1
fi

#
# generate the Altera EDIF netlist
#

if [ "$verbose" ]; then
	echo ""
else
	echo "" >> $logfile
fi

echo "Netlisting ..." | tee -a $logfile

options="-lib ${design}_lib -map ${design}.map -lmf ${maxLmfPath}"
options="$options -nc -pinlist ${design}.pinlist -proff ${maxPffPath}"
options="$options -out ${design}.edf"

if [ "$genACF" ]; then

	options="$options -acf ${design}.acf"

	if [ "$device" ]; then
		options="$options -dev $device"
	fi

	if [ "$family" ]; then
		options="$options -fam $family"
	fi

else

	if [ "$device" ]; then
		echo ""
		echo "Warning: device name is ignored because -genacf option not specified"
	fi

	if [ "$family" ]; then
		echo ""
		echo "Warning: family name is ignored because -genacf option not specified"
	fi
fi

if [ "$verbose" ]; then
	echo ""
	sir2alt $options $design 2>&1
else
	echo "" >> $logfile
	sir2alt $options $design >>$logfile 2>&1
fi

if [ $? != 0 ]; then
	echo ""
	echo "Error: Failed to netlist the design."
	exit 1
fi

#
# normal exit
#

echo "" | tee -a $logfile
echo "Done." | tee -a $logfile
exit 0
